package com.haoxuewa.config;

import com.haoxuewa.config.intercepors.LoginIntercepor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/*
 * 拦截器配置类
 * */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    @Autowired
    private LoginIntercepor loginIntercepor;
    @Value("${prop.upload-folder}")
    private String UPLOAD_FOLDER;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptor = registry.addInterceptor(loginIntercepor);
        //添加拦截路径
//        interceptor.addPathPatterns("/*");
        //添加不拦截路径
        List<String> list = new ArrayList<String>();
        list.add("/index");
        list.add("/login/*");
        list.add("/image/*");
        interceptor.excludePathPatterns(list);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //        addResourceHandler是指你想在url请求的路径
        // addResourceLocations是图片存放的真实路
        registry.addResourceHandler("/image/**")
                .addResourceLocations("file:" + UPLOAD_FOLDER);

        // 可以自定义资源处理类，对加载后的资源进行二次处理，比如图片统一打标识、解密之类的
        //.resourceChain(true).addTransformer(new SecretImageResourceTransformerSupport());
    }
}